package com.example.bailin.giftsay.me;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.login.LoginActivity;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.UrlProvide;


/**
 * Created by bailin on 16/8/15.
 */
public class MeFragment extends BaseFragment implements View.OnClickListener {

    private TabLayout tabLayout;
    private FrameLayout frameLayout;
    private SharedPreferences shared;
    private TextView tvName;

    @Override
    protected int setLayout() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initView() {
        tabLayout = bindView(R.id.tablayout_me);
        frameLayout = bindView(R.id.framelayout_me);
        ImageView ivLogin = bindView(R.id.iv_login);
        tvName = bindView(R.id.tv_login_name);
        ivLogin.setOnClickListener(this);

    }

    @Override
    protected void initData() {
        TabLayout.Tab tab = tabLayout.newTab();
        TabLayout.Tab tab1 = tabLayout.newTab();
        tab1.setText("单品");
        tabLayout.addTab(tab1);
        tab.setText("攻略");
        tabLayout.addTab(tab);
        tabLayout.getTabAt(0).select();

        initdata(0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                initdata(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        shared = MyApp.getContext().getSharedPreferences(UrlProvide.NAME, MyApp.getContext().MODE_PRIVATE);
        String name = shared.getString("username", "未登录");
        tvName.setText(name);
    }

    private void initdata(int position) {
        FragmentManager manager = getFragmentManager();
        switch (position) {
            case 0:
                manager.beginTransaction().replace(R.id.framelayout_me, new DanPFragment()).commit();
                break;
            case 1:
                manager.beginTransaction().replace(R.id.framelayout_me, new GongLFragment()).commit();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_login:
                String name = shared.getString("username", "未登录");
                if (name.equals("未登录")) {
                    Intent intent = new Intent(MyApp.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 102);
                } else {
                    Intent intent1 = new Intent(MyApp.getContext(), ExitActivity.class);
                    startActivityForResult(intent1, 103);
                }

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 102:
                if (resultCode == getActivity().RESULT_OK) {
                    tvName.setText(data.getStringExtra("username"));

                }
                break;
            case 103:
                if (resultCode == 911) {
                    String name = shared.getString("username", "未登录");
                    tvName.setText(name);
                }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
